package com.unohacha.qqj.fzdr.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.unohacha.qqj.fzdr.R;


/**
 * 作者：swt
 * 创建时间：2018/6/13
 * 描述：
 * 修订历史：
 */
public class MyAlertDialog extends Dialog implements View.OnClickListener {

    private TextView dialogContent, left, right, dialogTitle;
    private MyDialogClickListener mMyDialogClickListener;
    private String confirmText, cancleText, dialogContentText, title;
    private int titleColor = -1, contentColor = -1, leftColor = -1, rightColor = -1;
    private int titleSize = -1, contentSize = -1, leftSize = -1, rightSize = -1;

    public MyAlertDialog(Context context) {
        super(context, R.style.alert_dialog_center);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_custom);

        dialogTitle = findViewById(R.id.tv_title_dialog);
        dialogContent = findViewById(R.id.tv_dailog_content);
        left = findViewById(R.id.tv_dialog_left);
        right = findViewById(R.id.tv_dialog_right);

        if (TextUtils.isEmpty(title)) {
            dialogTitle.setVisibility(View.GONE);
        } else {
            dialogTitle.setVisibility(View.VISIBLE);
            dialogTitle.setText(title);
        }
        if (!TextUtils.isEmpty(dialogContentText)) {
            dialogContent.setText(dialogContentText);
        }
        if (!TextUtils.isEmpty(cancleText)) {
            left.setText(cancleText);
        }
        if (!TextUtils.isEmpty(confirmText)) {
            right.setText(confirmText);
        }
        if (titleSize != -1) {
            dialogTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, titleSize);
        }

        if (contentSize != -1) {
            dialogContent.setTextSize(TypedValue.COMPLEX_UNIT_SP, contentSize);
        }

        if (leftSize != -1) {
            left.setTextSize(TypedValue.COMPLEX_UNIT_SP, leftSize);
        }

        if (rightSize != -1) {
            right.setTextSize(TypedValue.COMPLEX_UNIT_SP, rightSize);
        }

        if (titleColor != -1) {
            dialogTitle.setTextColor(titleColor);
        }

        if (contentColor != -1) {
            dialogContent.setTextColor(contentColor);
        }

        if (leftColor != -1) {
            left.setTextColor(leftColor);
        }

        if (rightColor != -1) {
            right.setTextColor(rightColor);
        }


        left.setOnClickListener(this);
        right.setOnClickListener(this);
    }

    /**
     * 设置对话框标题
     *
     * @param title
     * @return
     */
    public MyAlertDialog setTitleText(String title) {
        this.title = title;
        return this;
    }

    /**
     * 设置对话框标题颜色
     *
     * @param titleColor
     * @return
     */
    public MyAlertDialog setTitleColor(int titleColor) {
        this.titleColor = titleColor;
        return this;
    }

    /**
     * 设置对话框标题大小
     *
     * @param titleSize
     * @return
     */
    public MyAlertDialog setTitleSize(int titleSize) {
        this.titleSize = titleSize;
        return this;
    }

    /**
     * 设置对话框内容
     *
     * @param content
     * @return
     */
    public MyAlertDialog setContentText(String content) {
        this.dialogContentText = content;
        return this;
    }

    /**
     * 设置对话框内容大小
     *
     * @param contentSize
     * @return
     */
    public MyAlertDialog setContentSize(int contentSize) {
        this.contentSize = contentSize;
        return this;
    }

    /**
     * 设置对话框内容颜色
     *
     * @param contentColor
     * @return
     */
    public MyAlertDialog setContentColor(int contentColor) {
        this.contentColor = contentColor;
        return this;
    }

    /**
     * 设置left按钮内容
     *
     * @param cancleText
     * @return
     */
    public MyAlertDialog setLeftText(String cancleText) {
        this.cancleText = cancleText;
        return this;
    }

    /**
     * 设置left按钮内容大小
     *
     * @param cancleText
     * @return
     */
    public MyAlertDialog setLeftSize(String cancleText) {
        this.cancleText = cancleText;
        return this;
    }

    /**
     * 设置left按钮内容颜色
     *
     * @param leftColor
     * @return
     */
    public MyAlertDialog setLeftColor(int leftColor) {
        this.leftColor = leftColor;
        return this;
    }

    /**
     * 设置right按钮内容
     *
     * @param confirmText
     * @return
     */
    public MyAlertDialog setRightText(String confirmText) {
        this.confirmText = confirmText;
        return this;
    }

    /**
     * 设置right按钮内容大小
     *
     * @param confirmText
     * @return
     */
    public MyAlertDialog setRightSize(String confirmText) {
        this.confirmText = confirmText;
        return this;
    }

    /**
     * 设置right按钮内容颜色
     *
     * @param rightColor
     * @return
     */
    public MyAlertDialog setRightColor(int rightColor) {
        this.rightColor = rightColor;
        return this;
    }

    /**
     * 设置触摸Dialog之外是否消失Dialog
     *
     * @param cancel
     * @return
     */
    public MyAlertDialog setCanceledOnTouchOut(boolean cancel) {
        this.setCanceledOnTouchOutside(cancel);
        return this;
    }

    /**
     * 设置监听
     *
     * @param myDialogClickListener
     * @return
     */
    public void setCustomDialogListener(MyDialogClickListener myDialogClickListener) {
        this.mMyDialogClickListener = myDialogClickListener;
        show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_dialog_left:
//                this.dismiss();
                if (mMyDialogClickListener != null) {
                    mMyDialogClickListener.clickLeft(this,null);
                }
                break;
            case R.id.tv_dialog_right:
//                this.dismiss();
                if (mMyDialogClickListener != null) {
                    mMyDialogClickListener.clickRight(this,null);
                }
                break;
            default:
                break;
        }
    }
}
